var a = getApp();

Page({
    data: {
        discoverInfo: [],
        isSotreKey: 1,
        discoverPage: 1,
        discoverLastPage: 0,
        discoverTap: !1,
        hotInfo: [],
        storeInfo: [],
        hotPage: 1,
        hotLastPage: 0,
        hotTap: !1,
        keyWord: "",
        searchHistory: []
    },
    onLoad: function(a) {
        var t;
        "" !== (null !== (t = a.searchPage) && void 0 !== t ? t : "") && this.setData({
            isSotreKey: Number(a.searchPage)
        }), wx.setNavigationBarTitle({
            title: "搜索"
        });
    },
    onReady: function() {},
    onShow: function() {
        3 === this.data.isSotreKey ? this.storeBaseData() : (this.getDiscover(), this.gethot());
        try {
            var a = wx.getStorageSync("searchHistory") || [];
            this.setData({
                searchHistory: JSON.parse(a)
            }), this.data.searchHistory[0] && this.setData({
                keyWord: this.data.searchHistory[0].name
            });
        } catch (a) {
            console.log(a);
        }
    },
    goSearch: function() {
        this.data.keyWord.length > 0 ? 3 === this.data.isSotreKey ? wx.navigateTo({
            url: "/packageF/consumption/search/search?keyword=" + this.data.keyWord
        }) : wx.navigateTo({
            url: "/packageB/member/category/search_v2/search_v2?keyword=" + this.data.keyWord
        }) : wx.navigateBack({
            delta: 1,
            fail: function(a) {}
        });
    },
    bindinp: function(a) {
        var t = a.detail.value;
        this.setData({
            keyWord: t
        });
    },
    tapKeyWord: function(a) {
        var t = a.currentTarget.dataset.name;
        this.setData({
            keyWord: t
        }), 3 === this.data.isSotreKey ? wx.navigateTo({
            url: "/packageF/consumption/search/search?keyword=" + this.data.keyWord
        }) : wx.navigateTo({
            url: "/packageB/member/category/search_v2/search_v2?keyword=" + this.data.keyWord
        });
    },
    deleHistory: function() {
        wx.setStorageSync("searchHistory", ""), this.setData({
            searchHistory: []
        });
    },
    colorFin: function(a) {
        a = (a = a.substring(1)).toLowerCase();
        for (var t = new Array(), e = 0; e < 3; e++) t[0] = a.substr(2 * e, 2), t[3] = "0123456789abcdef", 
        t[1] = t[0].substr(0, 1), t[2] = t[0].substr(1, 1), t[20 + e] = 16 * t[3].indexOf(t[1]) + t[3].indexOf(t[2]);
        return t[20] + "," + t[21] + "," + t[22];
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    tapUrl: function(a) {
        var t = a.currentTarget.dataset.item;
        try {
            wx.navigateTo({
                url: t.mini_url,
                fail: function(a) {
                    wx.switchTab({
                        url: t.mini_url
                    });
                }
            });
        } catch (a) {
            wx.switchTab({
                url: t.mini_url
            });
        }
    },
    gethot: function() {
        var t = this;
        wx.showLoading({
            title: "加载中"
        });
        var e = a.getNetAddresss("plugin.web-design.frontend.hot.index");
        a._postNetWork({
            url: e,
            data: {
                page: this.data.hotPage
            },
            success: function(a) {
                wx.hideLoading();
                var e = a.data;
                1 == e.result ? t.setData({
                    hotInfo: e.data.data,
                    hotLastPage: e.data.last_page,
                    hotTap: !0
                }) : t.setData({
                    hotInfo: !1,
                    hotTap: !1
                });
            },
            fail: function(a) {
                wx.hideLoading();
            }
        });
    },
    storeBaseData: function() {
        var t = this, e = a.getNetAddresss("plugin.coupon-store.frontend.search.searchBaseData");
        a._postNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    console.log(e);
                    for (var o = e.data.keyword, s = 0; s < o.length; s++) o[s].color = t.colorFin(o[s].color);
                    t.setData({
                        discoverInfo: o,
                        storeInfo: e.data.hot,
                        discoverLastPage: e.data.last_page,
                        discoverTap: !0
                    });
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    getDiscover: function() {
        var t = this;
        wx.showLoading({
            title: "加载中"
        });
        var e = a.getNetAddresss("plugin.web-design.frontend.discover.index");
        a._postNetWork({
            url: e,
            data: {
                page: this.data.discoverPage
            },
            success: function(a) {
                wx.hideLoading();
                var e = a.data;
                if (1 == e.result) {
                    for (var o = e.data.data, s = 0; s < o.length; s++) o[s].color = t.colorFin(o[s].color);
                    t.setData({
                        discoverInfo: o,
                        discoverLastPage: e.data.last_page,
                        discoverTap: !0
                    });
                } else t.setData({
                    discoverInfo: !1,
                    discoverTap: !1
                });
            },
            fail: function(a) {
                wx.hideLoading();
            }
        });
    },
    discoverMore: function() {
        var t = this;
        if (this.data.discoverTap && (this.data.discoverTap = !1, !(this.data.discoverPage >= this.data.discoverLastPage))) {
            this.setData({
                discoverPage: this.data.discoverPage + 1
            }), wx.showLoading({
                title: "加载中"
            });
            var e = a.getNetAddresss("plugin.web-design.frontend.discover.index");
            a._postNetWork({
                url: e,
                data: {
                    page: this.data.discoverPage
                },
                success: function(e) {
                    wx.hideLoading();
                    var o = e.data;
                    if (1 == o.result) {
                        for (var s = o.data.data, i = 0; i < s.length; i++) s[i].color = t.colorFin(s[i].color);
                        t.setData({
                            discoverInfo: t.data.concat(s),
                            discoverLastPage: o.data.last_page
                        }), t.data.discoverTap = !0;
                    } else t.setData({
                        discoverPage: t.data.discoverPage - 1
                    }), t.data.discoverTap = !0, a.tips(o.msg);
                },
                fail: function(a) {
                    wx.hideLoading();
                }
            });
        }
    },
    hotMore: function() {
        var t = this;
        if (this.data.hotTap && (this.data.hotTap = !1, !(this.data.hotPage >= this.data.hotLastPage))) {
            this.setData({
                hotPage: this.data.hotPage + 1
            }), wx.showLoading({
                title: "加载中"
            });
            var e = a.getNetAddresss("plugin.web-design.frontend.hot.index");
            a._postNetWork({
                url: e,
                data: {
                    page: this.data.hotPage
                },
                success: function(e) {
                    wx.hideLoading();
                    var o = e.data;
                    1 == o.result ? (t.setData({
                        hotInfo: t.data.hotInfo(o.data.data),
                        hotLastPage: o.data.last_page
                    }), t.data.hotTap = !0) : (t.setData({
                        hotPage: t.data.hotPage - 1
                    }), t.data.hotTap = !0, a.tips(o.msg));
                },
                fail: function(a) {
                    wx.hideLoading();
                }
            });
        }
    }
});